package plugins.openform;

import com.alibaba.excel.util.StringUtils;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.DistributeSessionlessCache;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.BeforeCreateListColumnsArgs;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;

/**
 * 标准单据列表插件
 */
public class OpenCardHistory extends AbstractListPlugin implements Plugin {

    // 使用分布式无会话缓存
    DistributeSessionlessCache cache = CacheFactory.getCommonCacheFactory().getDistributeSessionlessCache("customRegion");

    // 获取模式标签
    String modeltag = cache.get("Modeltag");
    // 获取缓存中的卡号
    String schoolCardNumber = cache.get("schoolcardnumber-toHistory");



    @Override
    public void beforeCreateListColumns(BeforeCreateListColumnsArgs args) {
        super.beforeCreateListColumns(args);

        // 动态隐藏指定字段
        if ("charge".equals(modeltag)) {

            args.getListColumns().stream()
                    .filter(col -> "dcb4_merchant.number".equals(col.getListFieldKey()))
                    .forEach(col -> col.setVisible(0));

        } else if ("spend".equals(modeltag)) {

            args.getListColumns().stream()
                    .filter(col -> "dcb4_user.name".equals(col.getListFieldKey()))
                    .forEach(col -> col.setVisible(0));

        }
    }

    public void setFilter(SetFilterEvent setFilterEvent) {
        super.setFilter(setFilterEvent);

        cache.remove("schoolcardnumber-toHistory"); // 清除缓存，避免重复使用
        // cache.remove("Modeltag"); // 清除缓存，避免重复使用

        // 获取入口参数
        FormShowParameter param = this.getView().getFormShowParameter();
        String model = param.getCustomParam("Model"); // 获取模式参数

        // 2. 添加过滤条件
        if (StringUtils.isNotBlank(schoolCardNumber)) {
            QFilter filter = new QFilter("dcb4_schoolcard.number", QCP.equals, schoolCardNumber);
            setFilterEvent.addCustomQFilter(filter);

            if ("charge".equals(modeltag) || "charge" .equals(model)) {

                // this.getView().showTipNotification("当前Model: " + modeltag);

                QFilter filter2 = new QFilter("dcb4_merchant.number", QCP.is_null,null ).and(new QFilter("dcb4_user.name", QCP.is_notnull, null));
                setFilterEvent.addCustomQFilter(filter2);
            } else if ("spend".equals(modeltag) || "spend".equals(model)) {

                // this.getView().showTipNotification("当前Model: " + modeltag);

                QFilter filter3 = new QFilter("dcb4_merchant.number", QCP.is_notnull, null ).and(new QFilter("dcb4_user.name", QCP.is_null, null));
                setFilterEvent.addCustomQFilter(filter3);
            }
        } else {
            if (model != null) { // 替换原来的!model.isEmpty()
                if ("charge".equals(model)) {
                    QFilter filter = new QFilter("dcb4_merchant.number", QCP.is_null, null).and(new QFilter("dcb4_user.name", QCP.is_notnull, null));
                    setFilterEvent.addCustomQFilter(filter);
                } else if ("spend".equals(model)) {
                    QFilter filter = new QFilter("dcb4_merchant.number", QCP.is_notnull, null).and(new QFilter("dcb4_user.name", QCP.is_null, null));
                    setFilterEvent.addCustomQFilter(filter);
                } else {
                    super.setFilter(setFilterEvent);
                }
            }
        }
    }
}